package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.model.domain.BlackList;
import com.itheima.model.domain.Question;
import com.itheima.model.domain.Settings;
import com.itheima.model.domain.User;
import com.tanhua.dubbo.mappers.*;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class SettingsImpl implements SettingsApi{

    @Autowired
    private SettingsMapper settingsMapper;

    @Autowired
    private QuestionMapper questionMapper;


    @Autowired
    private BlackListMapper blackListMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired

    private UserMapper userMapper;
    public BlackListMapper getBlackListMapper() {
        return blackListMapper;
    }

    @Override
    public Question question(Long userId) {
        QueryWrapper<Question> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
        return questionMapper.selectOne(qw);
    }

    @Override
    public Settings settings(Long userId) {
        QueryWrapper<Settings> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
        return settingsMapper.selectOne(qw);
    }

    @Override
    public Settings findByUserId(Long userId) {
        return null;
    }

    @Override
    public IPage blackFindByUserId(Long userId, int page, int size) {
        //1、构建分页参数对象Page
        Page pages = new Page(page,size);
        //2、调用方法分页（自定义编写 分页参数Page，sql条件参数）
        return userInfoMapper.findBlackList(pages,userId);
    }

    @Override
    public void save2(Settings settings) {
        settingsMapper.insert(settings);
    }

    @Override
    public void save(Question question) {
        questionMapper.insert(question);
    }

    @Override
    public void update(Question question) {
        questionMapper.updateById(question);
    }

    @Override
    public void update2(Settings settings) {
        settingsMapper.updateById(settings);
    }

    @Override
    public void deleteBlackList(Long userId, Long blackUserId) {
        QueryWrapper<BlackList> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
        qw.eq("black_user_id",blackUserId);
        blackListMapper.delete(qw);
    }
    @Override
    public User isMobileExit(String phone) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User :: getMobile ,phone);
        return userMapper.selectOne(wrapper);
    }
}
